package  
{
	import com.blitscreen.log.BlitDebug;	
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BlurFilter;
	import flash.geom.Rectangle;
	
	import com.blitscreen.render.BasicDrawPass;
	import com.blitscreen.render.BasicPostProcessPass;
	import com.blitscreen.render.BasicRenderQueue;
	import com.blitscreen.render.BlitSprite;
	import com.blitscreen.render.DrawPass;
	import com.blitscreen.render.PostProcessPass;
	import com.blitscreen.render.RenderQueue;
	import com.blitscreen.utils.RenderMonitorFields;
	import com.bourre.log.Logger;
	import com.bourre.utils.AirLoggerLayout;
	import com.kairos.utils.Monitor;
	import com.kairos.utils.MonitorColorUtils;
	import com.kairos.utils.MonitorFieldsList;		

	/**
	 * @author Cedric Nehemie
	 */
	 
	[SWF(width="300", height="500", frameRate="40", backgroundColor="#666666")]
	public class BlitScreenDemo1 extends Sprite 
	{
		protected var bitmap : Bitmap;		protected var bitmapData : BitmapData;	
		
		protected var renderQueue : RenderQueue;
		protected var sprites : Array;	

		public function BlitScreenDemo1 ()
		{
			Logger.getInstance().addLogListener( AirLoggerLayout.getInstance() );
			
			BlitDebug.DEBUG( MonitorColorUtils.print() );
			
			// creates the screen
			bitmapData = new BitmapData( 300, 300, false, 0x333333 );
			bitmap = new Bitmap( bitmapData );
			bitmap.y = 200;
			
			// the cleaner is used to erase the screen when a render start
			var cleaner : BlitSprite = new BlitSprite(); 
			cleaner.bitmap = bitmapData.clone();
			cleaner.area.width = 300;
			cleaner.area.height = 300;
			
			// creates render passes
			var drawPass : DrawPass = new BasicDrawPass ( true );
			drawPass.add( cleaner );
			
			// creates sprites for the draw pass
			sprites = new Array();
			for( var i : Number = 0; i < 50 ; i++ )
			{
				var s : BlitSprite = createBlitSprite();
				drawPass.add( s );
				sprites.push( s );
			}
			
			// Post-process pass
			var postProcessPass : PostProcessPass = new BasicPostProcessPass();
			postProcessPass.add( new BlurFilter( 5, 5, 2 ) );
			
			// creates the render queue and render one time
			renderQueue = new BasicRenderQueue();
			renderQueue.setOutput( bitmapData );			
			renderQueue.add( drawPass );
			//renderQueue.add( postProcessPass );
			renderQueue.render();
			
			// creates the monitor to display render details
			var monitor : Monitor = new Monitor( 290, 80 );
			
			monitor.addField( MonitorFieldsList.FPS );
			monitor.addField( MonitorFieldsList.MEMORY );			
			monitor.addField( RenderMonitorFields.RENDER( renderQueue, "Render" ) );			monitor.addField( RenderMonitorFields.PASSES( renderQueue, "Passes" ) );
			monitor.addField( RenderMonitorFields.DRAW( drawPass, "Draw" ) );
			monitor.addField( RenderMonitorFields.SPRITES( drawPass, "Sprites" ) );
			//monitor.addField( RenderMonitorFields.POST_PROCESS( postProcessPass, "Post-process" ) );
			monitor.start();
			
			// adds screen and monitor to stage
			addChild( bitmap );
			addChild( monitor );
			
			// creates animation handler
			addEventListener( Event.ENTER_FRAME, enterFrame );
		}
		
		public function enterFrame ( e : Event ) : void
		{
			for each( var ball : Ball in sprites )
			{
				ball.run();
			}
			
			renderQueue.render();
		}

		public function createBlitSprite () : BlitSprite 
		{
			var s : Shape = new Shape();
			
			s.graphics.beginFill( 0x666666 );
			s.graphics.lineStyle( 0, 0x999999 );
			s.graphics.drawCircle( 20, 20, 20 );
			s.graphics.endFill();
			
			var bmp : BitmapData = new BitmapData ( 40, 40, true, 0x00000000 );
			bmp.draw( s );	
					
			var blitSprite : BlitSprite = new Ball();
			blitSprite.bitmap = bmp;
			blitSprite.position.x = Math.random() * 260;
			blitSprite.position.y = Math.random() * 260;
			blitSprite.area.width = 40;
			blitSprite.area.height = 40;
			
			return blitSprite;
		}
	}
}

import flash.geom.Rectangle;

import com.blitscreen.render.BlitSprite;

internal class Ball extends BlitSprite
{
	private var speedX : Number = 1 - Math.random() * 2;
	private var speedY : Number = 1 - Math.random() * 2;
	
	public function run() : void
	{
		position.x += speedX;
		position.y += speedY;
		
		if( position.x + area.width > 300 )
		{
			speedX *= -1;
			position.x = 300 - area.width;
		}
		else if ( position.x < 0 )
		{
			speedX *= -1;
			position.x = 0;
		}
		
		if( position.y + area.height > 300 )
		{
			speedY *= -1;
			position.y = 300 - area.height;
		}
		else if ( position.y < 0 )
		{
			speedY *= -1;
			position.y = 0;
		}			
	}
}
